<template>
  <div class="xtx-form">
    <div class="user-info">
      <img :src="avatar" alt="头像" />
      <p>Hi，{{ nickName }} 欢迎来小兔鲜，完成绑定后可以QQ账号一键登录哦~</p>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-phone"></i>
        <input
          class="input"
          type="text"
          placeholder="绑定的手机号"
          v-model="form.mobile"
        />
      </div>
      <div class="error"></div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-code"></i>
        <input
          class="input"
          type="text"
          placeholder="短信验证码"
          v-model="form.code"
        />
        <span class="code" @click="send">{{
          time ? time + '秒后重试' : '发送验证码'
        }}</span>
      </div>
      <div class="error"></div>
    </div>
    <a href="javascript:;" class="submit" @click="submit">立即绑定</a>
  </div>
</template>

<script>
import QC from 'qc'
import { ref, reactive } from 'vue'
import { reqUserQQBindCode, reqUserQQBindLogin } from '@/api/user'
import { useCountDown } from '@/compositions'
import { message } from '@/components/Message'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
export default {
  name: 'CallbackBind',
  props: {
    openId: {
      type: String,
      default: ''
    }
  },
  setup (props) {
    // 设置基础信息
    const avatar = ref('')
    const nickName = ref('')
    // QC开头的方法都是QC第三方组件提供的 QC.Login.check() 验证是否登录成功
    if (QC.Login.check()) {
      // QC.api('get_user_info')  获取qq基本信息
      QC.api('get_user_info').success((res) => {
        // console.log(res)
        avatar.value = res.data.figureurl_qq_2
        nickName.value = res.data.nickname
      })
    }
    // 验证码
    const { time, start } = useCountDown()
    const form = reactive({
      mobile: '',
      code: ''
    })
    const send = async () => {
      if (!time.value) {
        if (!/^1[3-9]\d{9}$/.test(form.mobile)) {
          return message({
            type: 'error',
            text: '请输入正确手机号'
          })
        }
        const res = await reqUserQQBindCode(form.mobile)
        message({
          type: 'success',
          text: '发送成功'
        })
        start()
        console.log(res)
      }
    }
    const store = useStore()
    const router = useRouter()
    const submit = async () => {
      if (/^1[3-9]\d{9}$/.test(form.mobile) && form.code !== '') {
        const res = await reqUserQQBindLogin({
          ...form,
          unionId: props.openId
        })
        message({
          type: 'success',
          text: '绑定成功'
        })
        console.log('res----', res.result)
        store.commit('user/SET_PROFILE', res.result)
        router.push('/')
      }
    }
    return { avatar, nickName, time, send, form, submit }
  }
}
</script>

<style scoped lang='less'>
.user-info {
  width: 320px;
  height: 70px;
  margin: 0 auto;
  display: flex;
  background: #f2f2f2;
  align-items: center;
  padding: 0 10px;
  margin-bottom: 25px;
  img {
    background: #f2f2f2;
    width: 50px;
    height: 50px;
  }
  p {
    padding-left: 10px;
  }
}
.code {
  position: absolute;
  right: 0;
  top: 0;
  line-height: 50px;
  width: 80px;
  color: #999;
  &:hover {
    cursor: pointer;
  }
}
</style>
